'''
Created on Jan 26, 2012

@author: rarakar
'''
from asjp_util import cleanRow
concepts = {}

def getWALS(family):
	return family[0]

def getEthnologue(family):
	return family[1].split("@")[0]

def getHHClasf(family):
	return family[1].split("@")[1]

def getASJPDetails(f, fout):
	ethn = ""
	lang = "xxx"
	
	#Discard the first two lines
	f.readline()
	f.readline()
	#the first loop reads concept words
	for row in f:
		items = row.split()
		if not items:
			break
		concepts[items[0]] = items[1]
	#the second loops reads and discards the phonemes
	for row in f:
		if not row.split():
			break
	#the third loop reads language data
	for row in f:		
		if not row.split():
			continue
		if row[0].isupper():
			row = cleanRow(row)
			index = row.find("{")
			index2 = row.find("}")
			lang_details = row
			lang = row[0:index]
			family = row[index+1:index2].split('|')
			wals_clasf = getWALS(family)
			ethn_clasf = getEthnologue(family)
			hh_clasf = getHHClasf(family)			
			fout.write(lang+"\t"+wals_clasf+"\t"+ethn_clasf+"\t"+hh_clasf+"\t")			
		elif row.startswith(" "):
			extinct_age = 0
#			fam = ethn[0]
			line = row[1:]
			line = line.split()
			lat = row[2:10]
			lon = row[11:18]
			pop = row[18:30]
			iso = row[39:42].replace(" ","")
			wals = row[33:36]
			wals = wals.replace(" ","")
			extinct = pop.lstrip()
			if extinct == '':
				extinct_age = 0
			else:
				extinct_age = int(extinct)
			fout.write(lat+"\t"+lon+"\t"+extinct+"\t"+iso+"\t"+wals+"\n")			
		elif row[0].isdigit():			
			continue			
	return

if __name__ == '__main__':
	import codecs
	fin = codecs.open("listss14.txt", "r", "cp1252")
	fout = codecs.open("listss14-details.txt","w","cp1252")
	getASJPDetails(fin, fout)
	